package com.passioneffect.display {
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.AsyncErrorEvent;
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.text.TextField;

	import com.passioneffect.events.PEEvent;	

	/**
	 * @author Passion Effect
	 */
	public class VideoPlayer extends MovieClip {

		private var video : Video;
		private var videoConnection : NetConnection;
		private var netStream : NetStream;
		private var duration : Number;
		private var placeHolder : Sprite;
		private var _compWidth : Number = 0;
		private var _compHeight : Number = 0;

		private var previewTime : Number = 0;
		private var initHandler : Function;
		public var paused : Boolean;
		private var tf : TextField;
		private var resized : Boolean;

		public function VideoPlayer(compWidth : Number = 0,compHeight : Number = 0,previewTime : Number = 1) {
			this._compWidth = compWidth;
			this._compHeight = compHeight;
			
			//placeHolder = new Sprite();
			//drawPlaceHolder();
			//addChild(placeHolder);
		}

		public function load(source : String,initHandler : Function = null) : void {
			if(video) {
				removeChild(video);
				videoConnection = null;
				netStream = null;
			}
			video = new Video();
			videoConnection = new NetConnection();
			videoConnection.connect(null);
			netStream = new NetStream(videoConnection);
			
			/*
			if(!connectionString) {
			videoConnection.connect(null);
			}else {
			videoConnection.connect(connectionString);
			}
			 * 
			 */

			
			netStream.play(source);
			netStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
			netStream.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
			
			var client : Object = {};
			client.onMetaData = onMetaData;
			netStream.client = client;
			video.attachNetStream(netStream);
			
			this.initHandler = initHandler;
		}

		public function resumeOrPause() : void {
			if(paused) {
				resume();
			}else {
				pause();
			}
		}

		public function pause() : void {
			paused = true;
			netStream.pause();
		}

		public function resume() : void {
			paused = false;
			netStream.resume();
			this.addEventListener(Event.ENTER_FRAME, checkTime);
		}

		private function checkTime(event : Event) : void {
			if(netStream.time == this.duration) {
				
				netStream.pause();
				netStream.seek(previewTime);
				paused = true;
				
				dispatchEvent(new PEEvent(PEEvent.COMPLETE));
			}
		}

		private function onCuePoint(cuePoint : Object) : void {
			dispatchEvent(new PEEvent(PEEvent.CUE_POINT, 0, cuePoint));
		}

		private function onMetaData(metaData : Object) : void {
			this.duration = metaData.duration;	
			//netStream.seek(previewTime);
			netStream.pause();
			paused = true;
			//mask_eff.open();
			
			if(!resized){
				if(video.width - _compWidth > video.height - _compHeight) {
					//follow w
					video.scaleX = video.scaleY = _compWidth / video.width;
				}else {
					video.scaleX = video.scaleY = _compHeight / video.height;
				}
				resized=true;
			}
			
			addChild(video);
			if(initHandler != null)initHandler();
		}

		private function readTime(event : Event) : void {
		}

		private function asyncErrorHandler(event : AsyncErrorEvent) : void {
		}

		private function onStatus(event : NetStatusEvent) : void {
			if(tf && this.contains(tf))removeChild(tf);
			/*
			if(_autoScale && !scaled) {
				scaled = true;
				var s : Number;
				
				if(video.height - compHeight > video.width - compWidth) {
					//follow height
					s = compHeight / video.height;
					video.scaleY = video.scaleX = s;
				}else {
					s = compWidth / video.width;
					video.scaleY = video.scaleX = s;
				}
			}
			
			video.x = compWidth / 2 - video.width / 2;
			video.y = compHeight / 2 - video.height / 2;
			 * 
			 */
			
			//dispatchEvent(event);
		}

		public function get compWidth() : Number {
			return _compWidth;
		}

		public function set compWidth(compWidth : Number) : void {
			_compWidth = compWidth;
		}

		public function get compHeight() : Number {
			return _compHeight;
		}

		public function set compHeight(compHeight : Number) : void {
			_compHeight = compHeight;
		}
	}
}
